﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class WorkCenterBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IWorkCenterDA WorkCenterDA = DataAccess.CreateWorkCenterDA();

        public WorkCenterBLL()
        {
            this.pubParasInfo = null;
        }

        public WorkCenterBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public WorkCenterBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<WorkCenterInfo> workCenterInfos = new List<WorkCenterInfo>();
                WorkCenterInfo workCenterInfo = null;
                foreach (string id in IDS)
                {
                    workCenterInfo = WorkCenterDA.GetWorkCenterByID(dataBase, tran, id, this.dataBaseID[0]);
                    workCenterInfos.Add(workCenterInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (WorkCenterDA.DeleteWorkCenter(dataBase, tran, IDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<WorkCenterInfo>(dataBase, tran, workCenterInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "WORK_CENTER", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return WorkCenterDA.GetAllWorkCenter(this.dataBaseID[0]);
        }

        public WorkCenterInfo GetInfoByID(string workCenterID)
        {
            return WorkCenterDA.GetWorkCenterByID(workCenterID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(WorkCenterQueryEntity workCenterQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(workCenterQuery.WorkCenterID))
            {
                sqlWhere.Add(" WORK_CENTER_ID " + workCenterQuery.WorkCenterID);
            }
            if (!string.IsNullOrEmpty(workCenterQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + workCenterQuery.Descr);
            }
            if (!string.IsNullOrEmpty(workCenterQuery.Loc))
            {
                sqlWhere.Add(" LOC " + workCenterQuery.Loc);
            }
            if (!string.IsNullOrEmpty(workCenterQuery.LocInbound))
            {
                sqlWhere.Add(" LOC_INBOUND " + workCenterQuery.LocInbound);
            }
            if (!string.IsNullOrEmpty(workCenterQuery.LocOpComplete))
            {
                sqlWhere.Add(" LOC_OP_COMPLETE " + workCenterQuery.LocOpComplete);
            }
            if (!string.IsNullOrEmpty(workCenterQuery.LocFinishedGood))
            {
                sqlWhere.Add(" LOC_FINISHED_GOOD " + workCenterQuery.LocFinishedGood);
            }
            if (!string.IsNullOrEmpty(workCenterQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + workCenterQuery.IsActive);
            }
            return WorkCenterDA.GetWorkCenterByQueryList(sqlWhere, workCenterQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string workCenterID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                WorkCenterInfo workCenter = obj as WorkCenterInfo;
                if (WorkCenterDA.CheckWorkCenterIDUnique(dataBase, tran, workCenter.WorkCenterID, this.dataBaseID[0]))
                {
                    throw new Exception("WorkCenterID:" + workCenter.WorkCenterID + "已经存在,请重新输入!");
                }
                workCenter.UpdatedBy = workCenter.CreatedBy;
                workCenter.CreatedDate = DateTime.Now;
                workCenter.UpdatedDate = DateTime.Now;
                if (WorkCenterDA.InsertWorkCenter(dataBase, tran, workCenter, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<WorkCenterInfo>(dataBase, tran, null, workCenter, AuditAction.Insert, this.pubParasInfo.UserID, "WORK_CENTER", this.dataBaseID);
                    workCenterID = workCenter.WorkCenterID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return workCenterID;
        }

        public string Update(object obj)
        {
            string workCenterID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                WorkCenterInfo workCenter = obj as WorkCenterInfo;
                workCenter.UpdatedDate = DateTime.Now;
                WorkCenterInfo oldWorkCenterInfo = WorkCenterDA.GetWorkCenterByID(dataBase, tran, workCenter.WorkCenterID, this.dataBaseID[0]);
                if (WorkCenterDA.UpdateWorkCenter(dataBase, tran, workCenter, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<WorkCenterInfo>(dataBase, tran, oldWorkCenterInfo, workCenter, AuditAction.Update, this.pubParasInfo.UserID, "CARRIER", this.dataBaseID);
                    workCenterID = workCenter.WorkCenterID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return workCenterID;
        }
    }
}

